Passed
Push — master ( adc70c...8cf5a4 )
by Plamen
01:45
created

table_methods.LoadData.Run   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 14
rs 9.8
1
var table_methods = {
2
    helper: window.table_helper,
3
    Draw: {
4
        Section: function(tableContainer, dt, tSection){
5
            var section = tSection === "tfoot" ? "tfoot" : "tbody";
6
            var tSec = document.getElementById(tableContainer)
7
                        .getElementsByTagName(section)[0];
8
            Clear(tSec, this.helper.IePrior(9));
9
            for(var i = 0; i < dt.length; i++){
10
                var row = dt[i];
11
                var tRow = document.createElement("tr");
12
                Row(row, tRow);
13
                tSec.appendChild(tRow);
14
                if(section === "tfoot"){
15
                    this.helper.ProcessPaginationLinks(tSec);
16
                }
17
            }
18
            function Clear(tSection, iePrior9){
19
                if(iePrior9){
20
                    if(tSection.firstChild){
21
                        while(tSection.firstChild){
22
                            tSection.removeChild(tSection.firstChild);
23
                        }
24
                    }
25
                }else{
26
                    tSection.innerHTML = "";
27
                }
28
            }
29
            function Row(row, tRow){
30
                for(var cell in row){
1 ignored issue
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
31
                    var tCell = document.createElement("td");
32
                    if(typeof row[cell] === "string" ||
33
                            typeof row[cell] === "number"
34
                            ){
35
                        tCell.innerHTML = row[cell];
36
                    }else if(typeof row[cell] === "object"){
37
                        RowCellFromObject(row, cell, tCell);
38
                    }
39
                    tRow.appendChild(tCell);
40
                }
41
            }
42
            function RowCellFromObject(row, cell, tCell){
43
                for(var attr in row[cell]){
1 ignored issue
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
44
                    if(typeof row[cell][attr] === "string"){
45
                        tCell.innerHTML = row[cell][attr];
46
                    }else if(typeof row[cell][attr] === "object"){
47
                        for(var v in row[cell][attr]){
1 ignored issue
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
48
                            tCell.setAttribute(v, row[cell][attr][v]);
49
                        }
50
                    }
51
                }
52
            }
53
        },
54
        Run: function(tableContainer, d, instance){
55
56
            this.Section.call(instance, tableContainer, d.body);
57
            this.Section.call(instance, tableContainer, d.footer, "tfoot");
58
            if(instance.rq !== null){
59
                var hover = document.getElementById(instance.rq.tableId)
60
                        .getElementsByTagName("th")[instance.rq.colNo].lang;
61
                if(hover){
62
                    instance.helper.ColumnHover(tableContainer, instance.rq.colNo);
63
                }
64
            }
65
        }
66
    },
67
    Export: function(lnk, eType){
68
        var tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
69
        var rq = this.helper.BuildRequest.Run.call(this, tableId);
70
        rq.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ?
71
                eType :
72
                "csv";
73
        window.open(this.helper.RequestToUrl(rq));
74
        return false;
75
    },
76
    Filter: {
77
        Run: function(field){
78
            var tableId = this.Filter.GetTableId(field);
79
            if(tableId !== null){
80
                /*var exRq = this.rq;*/
81
                var rq = this.helper.BuildRequest.Run.call(this, tableId);
82
                this.rq = rq;
83
                this.LoadData.Run(this);
84
                //@todo exRq match not work
85
                /*if(exRq === null
86
                        || typeof this.rq.filer === "undefined"
87
                        || this.rq.filter !== exRq.filter
88
                        || this.rq.filterBy !== exRq.filterBy
89
                        ){
90
                    this.LoadData.Run(this);
91
                }*/
92
            }
93
        },
94
        GetTableId: function(field){
95
            if(field.tagName.toLowerCase() !== "select"){
96
                return field.getAttribute("data-table-id");
97
            }
98
            var f = field.parentNode.parentNode.getElementsByTagName("input")[0];
99
            return '' === f.value ? null : f.getAttribute("data-table-id");
100
        }
101
    },
102
    GoPage: {
103
        Run: function(lnk){
104
            var tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
105
            var rq = this.helper.BuildRequest.Run.call(this, tableId);
106
            rq.pageNo = this.GoPage.GetNo(lnk, rq.tableId);
107
            this.rq = rq;
108
            this.LoadData.Run(this);
109
            return false;
110
        },
111
        GetNo: function(lnk, tableId){
112
            //check & serve pagination jump links
113
            var jumpDir = lnk.innerHTML.trim().substr(0, 1);
114
            if(jumpDir === "+" || jumpDir === "-"){
115
                var current = document.getElementById(tableId)
116
                        .querySelector("tfoot .paging .a").innerHTML;
117
                var jump = lnk.innerHTML.replace("K", "000")
118
                        .replace("M", "000000000");
119
                var jumpPage = (parseInt(current) + parseInt(jump));
120
                lnk.parentNode.setAttribute("data-page", jumpPage);
121
                lnk.style.transform = "none";
122
            }
123
            return lnk.parentNode.hasAttribute("data-page") ?
124
                    lnk.parentNode.getAttribute("data-page") :
125
                    lnk.innerHTML;
126
        }
127
    },
128
    LoadData: {
129
        tail: null,
130
        Run: function(instance){
131
            if(this.tail !== null){
132
                this.tail.abort();
133
            }
134
            SetVisability(instance.rq.tableId, false);
135
            var inst = instance;
136
            var xmlhttp = window.XMLHttpRequest ?
137
                    new XMLHttpRequest() : //IE7+, Firefox, Chrome, Opera, Safari
138
                    new window.ActiveXObject("Microsoft.XMLHTTP");//IE6, IE5
139
            xmlhttp.onreadystatechange = function(){
140
                if(xmlhttp.readyState === 4 && xmlhttp.status === 200){
141
                    var d = JSON.parse(xmlhttp.responseText);
142
                    inst.Draw.Run(inst.rq.tableId, d, inst);
143
                    SetVisability(inst.rq.tableId, true);
144
                    window.table.LoadEndCalback(inst.rq.tableId);
145
                }
146
            };
147
            xmlhttp.open("GET", instance.helper.RequestToUrl(inst.rq), true);
148
            xmlhttp.send();
149
            this.tail = xmlhttp; //put at tail to can abort later previous request
150
            
151
            function SetVisability(tableContainer, flag){
152
                var tbl = document.getElementById(tableContainer);
153
                if(flag === true){
154
                    tbl.style.filter = "none";
155
                    tbl.style.opacity = "1";
156
                    tbl.style.cursor = "auto";
157
                }else if(flag === false){
158
                    tbl.style.filter = "blur(1px)";
159
                    tbl.style.opacity = "0.8";
160
                    tbl.style.cursor = "wait";
161
                }else{
162
                    console.error("table error in the flag value");
163
                }
164
            }
165
        }
166
    },
167
    ReloadData: function(tableId){
168
        var rq = this.helper.BuildRequest.Run.call(this, tableId);;
169
        this.rq = rq;
170
        this.LoadData.Run(this);
171
    },
172
    Sort: function(colNo, lnk){
173
        var tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
174
        var rq = this.helper.BuildRequest.Run.call(this, tableId);
175
        if(Math.round(colNo) === rq.colNo){
176
            rq.colOrd = (rq.colOrd === "asc" ? "desc" : "asc");
177
        }else{
178
            rq.colNo = Math.round(colNo);
179
            rq.colOrd = "asc";
180
        }
181
        this.rq = rq;
182
        //console.log(this,rq);
183
        this.LoadData.Run(this);
184
        //console.log(this.rq);
185
        ClearAndSetNewSortArrow(rq);
186
187
        function ClearAndSetNewSortArrow(rq){
188
            var headSpans = document.getElementById(rq.tableId)
189
                    .getElementsByTagName("thead")[0]
190
                    .getElementsByTagName("span");
191
            var length = headSpans.length;
192
            for(var i = 0; i < length; i++){
193
                headSpans[i].innerHTML = "";
194
            }
195
            lnk.getElementsByTagName("span")[0].innerHTML =
196
                    (rq.colOrd === "desc" ? rq.strDesc : rq.strAsc);
197
        }
198
    }
199
};
200